package com.shop.web.menu.congtroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.annotation.Auth;
import com.shop.utils.ResultUtils;
import com.shop.utils.ResultVo;
import com.shop.web.menu.entity.MakeMenuTree;
import com.shop.web.menu.entity.Menu;
import com.shop.web.menu.service.MenuService;
import com.shop.web.admin_menu.entity.AssignParm;
import com.shop.web.menu.entity.PermissonVo;
import com.shop.web.admin.entity.Admin;
import com.shop.web.admin.service.AdminService;
import com.shop.web.admin_menu.service.AdminMenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMenuService adminMenuService;

    //新增
    @Auth
    @PostMapping
    public ResultVo add(@RequestBody Menu menu){
        menu.setCreateTime(new Date());
        if(menuService.save(menu)){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

     //编辑
    @Auth
    @PutMapping
    public ResultVo edit(@RequestBody Menu menu){
        if(menuService.updateById(menu)){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

      //删除
    @Auth
    @DeleteMapping("/{menuId}")
    public ResultVo edit(@PathVariable("menuId") Long menuId){
        if(menuService.removeById(menuId)){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @GetMapping("/list")
    @Auth
    public ResultVo list(){
        QueryWrapper<Menu> query = new QueryWrapper<>();
        query.lambda().orderByAsc(Menu::getOrderNum);
        List<Menu> menuList = menuService.list(query);
        //组装树数据
        List<Menu> list = MakeMenuTree.makeTree(menuList, 0L);
        return ResultUtils.success("查询成功!",list);
    }

     //查询上级菜单
    @GetMapping("/getParent")
    @Auth
    public ResultVo getParent(){
        List<Menu> list = menuService.getParnet();
        return ResultUtils.success("查询成功!",list);
    }

    //分配菜单树数据查询和回显
    @GetMapping("/getAssignTree")
    @Auth
    public ResultVo getAssignTree(Long userId,Long assId){
        //查询当前用户的信息
        Admin user = adminService.getById(userId);
        //查询菜单信息
        List<Menu> menuList = null;
        //判断用户是否是超级管理员，超级管理员拥有所有的权限
        if(StringUtils.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())){
             menuList = menuService.list();
        }else{
            //根据用户id查询
            menuList = menuService.getMenuByUserId(userId);
        }
        //组装树数据
        List<Menu> menus = MakeMenuTree.makeTree(menuList, 0L);
        //设置菜单数据
        PermissonVo vo = new PermissonVo();
        vo.setMenuList(menus);
        //查询回显的数据
        List<Menu> menuByUserId = menuService.getMenuByUserId(assId);
        List<Long> ids = new ArrayList<>();
        Optional.ofNullable(menuByUserId).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null)
                .forEach(item ->{
                    ids.add(item.getMenuId());
                });
        vo.setCheckList(ids.toArray());
        return ResultUtils.success("查询成功！",vo);
    }

    //保存菜单
    @Auth
    @PostMapping("/assignSave")
    public ResultVo assignSave(@RequestBody AssignParm parm){
        //判断如果是超级管理员，不用分配
        Admin user = adminService.getById(parm.getAssId());
        if(StringUtils.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())){
            return ResultUtils.error("不能为超级管理员分配菜单!");
        }
        adminMenuService.saveMenu(parm);
        return ResultUtils.success("分配成功!");
    }

}
